package com.oggero.glossarium.persistence.dao;

import com.oggero.glossarium.persistence.common.DaoDbBase;
import com.oggero.glossarium.persistence.entities.VocaboloEntity;
import com.oggero.glossarium.persistence.mappers.VocaboloMapper;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 *
 * @author Alessio
 */
public class VocaboloDao implements DaoDbBase {

//    private EntityManager entityManager;
    private HibernateTemplate sessionFactory;
    private VocaboloMapper vocaboloMapper;
    private Logger logger = Logger.getLogger(VocaboloDao.class.getName());

    /**
     * 
     * @param objectToInsert
     * @return 
     */
    public Object insert(Object objectToInsert) throws Exception {
        VocaboloEntity vocaboloEntity = vocaboloMapper.convertToEntity(objectToInsert);
        logger.log(Level.INFO, vocaboloEntity.toString());
        sessionFactory.persist(vocaboloEntity);
        return objectToInsert;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = new HibernateTemplate(sessionFactory);
//        logger.log(Level.INFO, "CREATO SESSIONFACTORY");
    }

//    @PersistenceContext
//    public void setEntityManager(EntityManager entityManager) {
//        this.entityManager = entityManager;
//    }

    public void setVocaboloMapper(VocaboloMapper vocaboloMapper) {
        this.vocaboloMapper = vocaboloMapper;
    }
}
